package com.service.impl;

import com.common.exception.db.InsertException;
import com.common.paging.PagingData;
import com.dao.GwDao;
import com.dto.in.Gw_YY;
import com.entity.GwYYInfo;
import com.service.GwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/3/15 0015.
 */
@Service
public class GwServiceImpl implements GwService{
    @Autowired
    private GwDao dao;

    @Override
    @Transactional
    public int insertYY(String phone, String yclass, String subjectId) {
        int count = dao.findPhoneIsBeing(phone);
        if(count == 0){
            Gw_YY gy = new Gw_YY();
            gy.setPhone(phone);
            gy.setYclass(yclass);
            count = dao.inserYY(gy);
            if(count > 0){
                String[] sub = subjectId.split(",");
                for (int i = 0; i < sub.length; i++) {
                    int res = dao.addYYSubject(gy.getId(),sub[i]);
                    if(res == 0){
                        throw new InsertException("新增失败");
                    }
                }
                return 1;
            }else{
                throw new InsertException("新增失败");
            }
        }else{
            return -1;
        }
    }

    @Override
    public PagingData findYY(Integer page, Integer length) {
        List<GwYYInfo> list = dao.findYY(page,length);
        int count = dao.findYYCount();
        for(GwYYInfo info : list){
           List<String> list1 = dao.findSubjectName(info.getId());
           info.setSubjectName(list1);
        }

        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return pagingData;
    }




}
